<?php
namespace app\home\controller;

use app\common\model\Lottery3Log;

use app\common\component\user\UserService;
use app\common\component\user_wallet\UserWalletService;
use app\common\component\user_wallet\UserWalletModel;
use app\common\component\user_wallet_log\UserWalletLogService;
use app\common\component\lottery_3_log\Lottery3LogService;

class GameLotteryThird extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();
	}

    public function index() {
    	$currentLottery3Log = (new Lottery3LogService()) -> getInterest();
		$this -> view -> assign('currentLottery3LogPrice', $currentLottery3Log['data']['price']);

		$this -> view -> assign('lottery3Logs', Lottery3Log::where('user_id', \think\Session::get('user_id')) -> order('status ASC, lottery_3_log_id DESC') -> paginate(10, true));

    	return $this -> view -> fetch();
	}

	public function sale() {
		$user_id = \think\Session::get('user_id');
		$selectLottery3Log = Lottery3Log::where('status in(3, 4) AND interest > 0 AND user_id = ' . $user_id) -> order('lottery_3_log_id ASC') -> select();
		if(!empty($selectLottery3Log)) {
			\think\Db::startTrans();
			try {
				$price = 0;

				foreach($selectLottery3Log as $lottery3Log) {
					$price += $lottery3Log -> interest;

					switch($lottery3Log -> getData('status')) {
						case 4:
							$lottery3Log -> status = 9;
							$lottery3Log -> last_get_interest_time = $lottery3Log -> end_time;
							break;
						case 3:
							$remainingDays = countDays($lottery3Log -> last_get_interest_time);
							$totalDays = countDays($lottery3Log -> last_get_interest_time, $lottery3Log -> end_time);
							$lottery3Log -> last_get_interest_time = $lottery3Log -> end_time - ($totalDays - $remainingDays) * 24 * 60 * 60;
							break;
					}

					$lottery3Log -> interest = 0;

					if(!$lottery3Log -> save()) {
						throw new \Exception('鹅蛋出售失败, lottery3Log');
					}
				}

				if(!UserWalletModel::where('user_id', \think\Session::get('user_id')) -> setInc('money', $price)) {
					throw new \Exception('出售鹅蛋,失败, userWallet');
				}

				$user_name = \think\Session::get('user_name');
				$resultUserWalletLog = (new UserWalletLogService) -> log(5, 1, $price, $user_name, '出售鹅蛋', 1, $user_name);
				if($resultUserWalletLog['code'] !== 0) {
					throw new \Exception($resultUserWalletLog['message']);
				}

				\think\Db::commit();
			} catch(\Exception $e) {
				\think\Db::rollback();
	
				echo '<meta charset="utf-8"><script>alert("' . $e -> getMessage() . '!");window.history.go(-1);</script>';exit;
			}

			echo '<meta charset="utf-8"><script>alert("出售成功!");window.location.href="/game/lottery/third";</script>';exit;
		}
		echo '<meta charset="utf-8"><script>alert("您没有鹅蛋可出售!");window.history.go(-1);</script>';exit;
	}

    public function buy() {
    	// 金钱钱包
    	$resultUserWallet = (new UserWalletService) -> getUserWallet();
    	$this -> view -> assign('user_wallet', $resultUserWallet['data']['user_wallet']);

		return $this -> view -> fetch();
	}

    public function handleBuy() {
    	$post = $this -> request -> post();
		$post['price'] = floor($post['price']);

		$resultUserWallet = (new UserWalletService) -> getUserWallet();
		if($post['price'] > $resultUserWallet['data']['user_wallet'] -> money) {
    		echo '<meta charset="utf-8"><script>alert("您的金钱不足!");window.history.go(-1);</script>';exit;
    	}

		$resultCheckPayPassword = (new UserService()) -> checkPayPassword($post['pay_password']);
    	if($resultCheckPayPassword['code'] !== 0) {
    		echo '<meta charset="utf-8"><script>alert("' . $resultCheckPayPassword['message'] . '!");window.history.go(-1);</script>';exit;
    	}

		\think\Db::startTrans();
		try {

			$resultUserWallet['data']['user_wallet'] -> money -= $post['price'];
			if(!$resultUserWallet['data']['user_wallet'] -> save()) {
				throw new \Exception('金额或提现额度扣除失败');
			}

			$user_name = \think\Session::get('user_name');
			$resultUserWalletLog = (new UserWalletLogService) -> log(7, 0, $post['price'], $user_name, '购买天鹅', 1, $user_name);
			if($resultUserWalletLog['code'] !== 0) {
				throw new \Exception($resultUserWalletLog['message']);
			}

			$resultLottery3Log = (new Lottery3LogService()) -> makeLog(\think\Session::get('user_id'), $post['price']);
			if($resultLottery3Log['code'] !== 0) {
				throw new \Exception($resultLottery3Log['message']);
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();

			echo '<meta charset="utf-8"><script>alert("' . $e -> getMessage() . '!");window.history.go(-1);</script>';exit;
		}

		echo '<meta charset="utf-8"><script>alert("购买成功!");window.location.href="/game/lottery/third";</script>';exit;
	}
}